package com.xiashitech.authservice.service;

import com.google.auto.service.AutoService;
import com.tencent.bk.sdk.iam.dto.InstanceDTO;
import com.tencent.bk.sdk.iam.dto.PathInfoDTO;
import com.tencent.bk.sdk.iam.util.PathBuilder;
import com.xiashitech.authservice.config.AuthHelperCache;
import com.xiashitech.authservice.constants.ActionId;
import com.xiashitech.authservice.constants.ResourceTypeEnum;
import com.xiashitech.authservice.constants.ResourceTypeId;
import com.xiashitech.authservice.util.BeanCopierUtil;
import com.xiashitech.authservice.util.IamAuthUtil;
import com.xiashitech.interfaces.authservice.AuthService;
import com.xiashitech.interfaces.authservice.dto.AuthDTO;
import lombok.NonNull;

import java.util.ArrayList;
import java.util.List;

@AutoService(AuthService.class)
public class VerifyAuthService implements AuthService {
    @Override
    public AuthDTO verifyAuth(AuthDTO authDTO) {
        AuthDTO authReturnDTO = null;
        AuthHelperCache.initialAuthHelper(authDTO.getSystemId(),authDTO.getAppCode(),authDTO.getAppSecret(),authDTO.getIamBaseUrl());
        if(authDTO.getColumnName() != null) {
            authReturnDTO = authColumn(authDTO.getSystemId(),authDTO.getUserId(),authDTO.getDatabaseService(),authDTO.getDatabaseName(),authDTO.getDatabaseSchema(),authDTO.getTableName(),authDTO.getColumnName(),authDTO.getActionId());
        } else if(authDTO.getTableName() != null) {
            authReturnDTO = authTable(authDTO.getSystemId(),authDTO.getUserId(),authDTO.getDatabaseService(),authDTO.getDatabaseName(),authDTO.getDatabaseSchema(),authDTO.getTableName(),authDTO.getActionId());
        }
        if(authReturnDTO != null) {
            authDTO.setAuth(authReturnDTO.isAuth());
            authDTO.setIsAllowedResources(authReturnDTO.getIsAllowedResources());
            authDTO.setResult(authReturnDTO.getResult());
        }
        return authDTO;
    }

    private AuthDTO authTable(String systemId, String username, String databaseService, String databaseName, String databaseSchema, String tableName, String actionId) {
        String database = String.format("%s.%s", databaseService, databaseName);
        String schema = String.format("%s.%s", database, databaseSchema);
        String table = String.format("%s.%s", schema, tableName);
        actionId = table + "_" + actionId.toLowerCase();
        AuthDTO authDTO = null;
        try {
            List<InstanceDTO> instanceList = new ArrayList();
            PathInfoDTO pathInfoDTO = IamAuthUtil.buildTablePathInfoDTO(databaseService,databaseName,databaseSchema,tableName);
            instanceList.add(IamAuthUtil.buildInstance(table,table,systemId,pathInfoDTO));
            Object authResultObject = AuthHelperCache.getCacheAuthHelpers().get(systemId).isAllowed(username, actionId,instanceList);
            authDTO = BeanCopierUtil.copy(authResultObject, AuthDTO.class);
            if(authDTO.getIsAllowedResources() != null && authDTO.getIsAllowedResources().size() > 0) {
                authDTO.setAuth(true);
            }
        } catch (Exception e) {
            authDTO = new AuthDTO();
            authDTO.setAuth(false);
        }

        return authDTO;
    }

    private AuthDTO authColumn(String systemId, String username, String databaseService, String databaseName, String databaseSchema, String tableName, String columnName, String actionId) {
        String database = String.format("%s.%s", databaseService, databaseName);
        String schema = String.format("%s.%s", database, databaseSchema);
        String table = String.format("%s.%s", schema, tableName);
        String column = String.format("%s.%s", table, columnName);
        actionId = table + "_" + actionId.toLowerCase();

        AuthDTO authDTO = null;
        try {
            List<InstanceDTO> instanceList = new ArrayList();
            PathInfoDTO pathInfoDTO = IamAuthUtil.buildTablePathInfoDTO(databaseService,databaseName,databaseSchema,tableName);
            instanceList.add(IamAuthUtil.buildInstance(table,table,systemId,pathInfoDTO));
            pathInfoDTO = IamAuthUtil.buildColumnPathInfoDTO(databaseService,databaseName,databaseSchema,tableName,columnName);
            instanceList.add(IamAuthUtil.buildInstance(table + "-" + ResourceTypeEnum.COLUMN.getId(),column,systemId,pathInfoDTO));
            Object authResultObject = AuthHelperCache.getCacheAuthHelpers().get(systemId).isAllowed(username, actionId,instanceList);
            authDTO = BeanCopierUtil.copy(authResultObject, AuthDTO.class);
            if(authDTO.getIsAllowedResources() != null && authDTO.getIsAllowedResources().size() > 0) {
                authDTO.setAuth(true);
            }
        } catch (Exception e) {
            authDTO = new AuthDTO();
            authDTO.setAuth(false);
        }

        return authDTO;
    }
}